#include "soc.h"
#include "stdlix.h"
/*
 *获取HTTP请求的协议
 */
int getRequestHTTPHead(char * dest)
{
	const char * str =
			"POST /index.php HTTP/1.1\r\n"
			"Host: ["SERVER_ADDRESS"]:8080\r\n"
			"Content-type: test/plain\r\n"
			"Content-length: 19\r\n\r\n";
	/*if(dest !=NULL)
	{
		bzero(dest,sizeof(a));
	*/
	strcpy(dest,str);
//	}
	return strlen(dest);
}
/*
 * 获取HTTP响应的协议
 */
int getReponseHTTPHead(char * dest)
{
	const char * str =
			"HTTP/1.0 200 OK\r\n"
			"Server: Apache 2.2.6\r\n"
			"Content-type: test/plain\r\n"
			"Content-length: 100\r\n"
			"Connection: Close\r\n\r\n";

	strcpy(dest,str);
	return strlen(dest);
}
int send_to_server(ServerMessage* mes,char* ipv6_addr)
{
    char buff[150];
    int sockfd,headcount = 0,i = 0;
//    strcpy(buff,(char*)mes);
    struct sockaddr_in6 serveraddr;
    bzero(buff,120);
    sockfd=socket(AF_INET6,SOCK_STREAM,0);
//    printf("socket is %d\n",sockfd);
    bzero(&serveraddr,sizeof(serveraddr));
    serveraddr.sin6_family=AF_INET6;
    serveraddr.sin6_port=htons(SERVER_PORT);
    inet_pton(AF_INET6,ipv6_addr,&serveraddr.sin6_addr);
    if(connect(sockfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr))==-1)
    {   
//    	perror("connect");
        close(sockfd);
        return FALSE;
    }
/*    printf("*** %d %d %d %d %d %d ***\n",mes[0],mes[1],mes[2],mes[3],mes[4],mes[5]);
    printf("*** %d %d %d %d %d %d ***\n",
    		mes->order,mes->data[0],mes->data[1],mes->data[2],mes->data[3],mes->data[4]);
    printf("*** %d %d %d %d ***\n",
    		mess->order,mess->num,mess->temperature,mess->humidity);*/
    headcount = getRequestHTTPHead(buff);
//    printf("headcount:%d\n",headcount);
//    printf("get head %d\n",headcount);
    buff[headcount] = mes->order;
//    printf("buff[headcount] = %d & mes->order = %d\n",buff[headcount],mes->order);
    for(i = 0;i<SERVER_MESSAGE_DATA_LENGTH;i++)
    	buff[headcount+1+i]=mes->data[i];
//    printf("head %100s\n",buff);
/*
    printf("*** send %d %d %d %d %d %d ***\n",
    		buff[headcount],buff[headcount+1],buff[headcount+2],buff[headcount+3],buff[headcount+4],buff[headcount+5]);
*/
   if(-1 == send(sockfd,buff,headcount+i-1,0)/*||-1==send(sockfd,buffmes,sizeof(buffServerData),0)*/)
	 {
//		 perror("send");
		 close(sockfd);
		 return FALSE;
	 }
    close(sockfd);
    return TRUE;

}

